package com.example.yuejian.yuejian.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.UsersInfo;
import com.example.yuejian.yuejian.dialog.UpdateDialog;
import com.example.yuejian.yuejian.listener.SexListener;
import com.example.yuejian.yuejian.model.UserModel;
import com.example.yuejian.yuejian.view.RoundImageView;
import com.squareup.picasso.Picasso;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;

public class UserInfoActivity extends BaseActivity {

    private TextView username;
    private TextView usergender;
    private  TextView useraddress;
    private LinearLayout gender;
    private LinearLayout name;
    private LinearLayout address;
    private RoundImageView userphoto;

    private static final int PHOTO_REQUEST_CAREMA = 1;
    private static final int PHOTO_REQUEST_GALLERY = 2;
    private static final int PHOTO_REQUEST_CUT = 3;
    private static final String PHOTO_FILE_NAME = "temp_photo.jpg";
    private File tempFile;
    private File file;

    private ImageView btn_back;
    int sexId;
    private SexListener listener=new SexListener() {
        @Override
        public void onSuccess(String sex,int gender) {
            usergender.setText(sex);
            sexId =gender;
        }
    };

    private ModleListener<UsersInfo> userinfoListener=new ModleListener<UsersInfo>() {
        @Override
        public void onResponse(UsersInfo o) {
            UsersInfo res=o;

            sexId =res.getGender();
            if (!"".equals(res.getPhoto()))
                Picasso.with(UserInfoActivity.this).load(res.getPhoto()).into(userphoto);
            username.setText(res.getNickname());
            useraddress.setText(res.getAddress());
            if (1==res.getGender())
            {
                usergender.setText("女");
            }
            else
            {
                usergender.setText("男");
            }
               /* userProvince.setText(res.getProvince());
                usercity.setText(res.getCity());
                userschool.setText(res.getSchool());*/
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),msg,Toast.LENGTH_SHORT).show();
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_info);
        initViews();
        initEvets();
        back();
    }
    private void initViews()
    {
        userphoto=(RoundImageView) findViewById(R.id.person_userphoto_img);
        username=(TextView)findViewById(R.id.person_username_content);
        usergender=(TextView)findViewById(R.id.person_gender_content);

        useraddress=(TextView)findViewById(R.id.person_address_content);
        gender=(LinearLayout)findViewById(R.id.person_linear_gender);
        name=(LinearLayout)findViewById(R.id.person_linear_name);
        address=(LinearLayout)findViewById(R.id.person_linear_address);

    }
    private void  getDatas()
    {
;
        UserModel userModel=new UserModel();
        userModel.userInfo(getUser_id(),userinfoListener);


    }
    public  void initEvets()
    {
        final UpdateDialog dialog=new UpdateDialog();
        gender.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.select(UserInfoActivity.this,sexId,listener,getUser_id());
                // select();
            }
        });
      name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(UserInfoActivity.this,EditnameActivity.class);
                intent.putExtra("user_name",username.getText().toString());
                startActivity(intent);
            }
        });
       address.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent =new Intent(UserInfoActivity.this,EditAddressActivity.class);
                intent.putExtra("address",useraddress.getText().toString());
                startActivity(intent);
            }
        });
        userphoto.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_PICK);
                intent.setType("image/*");
                startActivityForResult(intent,
                        PHOTO_REQUEST_GALLERY);
            }
        });


    }
    private void back()
    {
        btn_back=(ImageView)findViewById(R.id.imgBtn_person_center_back);
        btn_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    @Override
    protected void onResume() {
        getDatas();
        super.onResume();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PHOTO_REQUEST_GALLERY) {
            if (data != null) {

                Uri uri = data.getData();
                Log.e("图片路径？？", data.getData() + "");
                //tempFile= uri2File(uri);
                //userphoto.setImageURI(uri);
               // crop(uri);
                // file = new File(uri.toString());
            }

        }
        else if (requestCode == PHOTO_REQUEST_CUT) {
            if (data != null) {
                final Bitmap bitmap = data.getParcelableExtra("data");
                userphoto.setImageBitmap(bitmap);
                // 保存图片到internal storage
                FileOutputStream outputStream;
                try {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
                    out.flush();
                    // out.close();
                    // final byte[] buffer = out.toByteArray();
                    // outputStream.write(buffer);
                    outputStream = UserInfoActivity.this.openFileOutput("_head_icon.jpg", Context.MODE_PRIVATE);
                    out.writeTo(outputStream);
                    out.close();
                    outputStream.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                if (tempFile != null && tempFile.exists())
                   file=tempFile;
                    tempFile.delete();
            } catch (Exception e) {
                e.printStackTrace();
            }

        }
    }

    private void crop(Uri uri) {
        Intent intent = new Intent("com.android.camera.action.CROP");
        intent.setDataAndType(uri, "image/*");
        intent.putExtra("crop", "true");
        intent.putExtra("aspectX", 1);
        intent.putExtra("aspectY", 1);
        intent.putExtra("outputX", 250);
        intent.putExtra("outputY", 250);
        intent.putExtra("outputFormat", "JPEG");
        intent.putExtra("noFaceDetection", true);
        intent.putExtra("return-data", true);
        startActivityForResult(intent, PHOTO_REQUEST_CUT);
    }

}
